package com.bl.student.service.biz.comment.impl;

import com.bl.student.common.ApiResponseCode;
import com.bl.student.common.CommonServiceException;
import com.bl.student.common.utils.UUIDUtils;
import com.bl.student.dao.model.CommentEntity;
import com.bl.student.dao.model.TaskEntity;
import com.bl.student.dao.model.TeacherEntity;
import com.bl.student.dao.model.UserEntity;
import com.bl.student.dao.redis.model.UserInfo;
import com.bl.student.dao.repository.CommentRepository;
import com.bl.student.dao.repository.TaskRepository;
import com.bl.student.dao.repository.TeacherRepository;
import com.bl.student.dao.repository.UserRepository;
import com.bl.student.service.biz.comment.CommentService;
import com.bl.student.service.biz.teacher.TeacherService;
import com.bl.student.service.vo.DelBody;
import com.bl.student.service.vo.DelListBody;
import com.bl.student.service.vo.comment.CommentAddVo;
import com.bl.student.service.vo.comment.CommentListVo;
import com.bl.student.service.vo.comment.CommentVo;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import org.springframework.data.domain.Pageable;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by liangweikang on 18/2/2.
 */
@Service
@Log4j2
public class CommentServiceImpl implements CommentService{

    @Autowired
    CommentRepository commentRepository;

    @Autowired
    UserRepository userRepository;

    @Autowired
    TeacherRepository teacherRepository;

    @Autowired
    TaskRepository taskRepository;

    @Transactional
    @Override
    public void addCommentToB(String uid,CommentAddVo commentAddVo) {

        // uid 为甲方 id
        CommentEntity commentEntity = new CommentEntity();
        commentEntity.setContent(commentAddVo.getContent());
        commentEntity.setId(UUIDUtils.getUUID());
        commentEntity.setCreatorId(uid);
        commentEntity.setType(commentAddVo.getType());
        commentEntity.setTaskId(commentAddVo.getTaskId());
        commentEntity.setPoint(commentAddVo.getPoint());

        UserEntity userEntity = userRepository.findOne(commentAddVo.getReceiverId());
        commentEntity.setReceiverId(userEntity.getId());
        commentEntity.setReceiverNickname(userEntity.getNickname());
        commentEntity.setReceiverAvatar(userEntity.getAvatar());

        TeacherEntity teacherEntity = teacherRepository.findOne(uid);
        commentEntity.setCreatorId(uid);
        commentEntity.setCreatorNickname(teacherEntity.getNickname());
        commentEntity.setCreatorAvatar(teacherEntity.getAvatar());

        commentEntity.setUpdateTime(new Date());
        commentEntity.setCreateTime(new Date());
        commentRepository.save(commentEntity);
    }

    @Override
    public void addCommentToA(String uid, CommentAddVo commentAddVo) {

        // uid 为乙方id
        CommentEntity commentEntity = new CommentEntity();
        commentEntity.setContent(commentAddVo.getContent());
        commentEntity.setId(UUIDUtils.getUUID());
        commentEntity.setCreatorId(uid);
        commentEntity.setType(commentAddVo.getType());
        commentEntity.setTaskId(commentAddVo.getTaskId());
        commentEntity.setPoint(commentAddVo.getPoint());

        TeacherEntity teacherEntity = teacherRepository.findOne(commentAddVo.getReceiverId());
        commentEntity.setReceiverId(commentAddVo.getReceiverId());
        commentEntity.setReceiverNickname(teacherEntity.getNickname());
        commentEntity.setReceiverAvatar(teacherEntity.getAvatar());

        UserEntity userEntity = userRepository.findOne(uid);
        commentEntity.setCreatorId(uid);
        commentEntity.setCreatorNickname(userEntity.getNickname());
        commentEntity.setCreatorAvatar(userEntity.getAvatar());

        commentEntity.setUpdateTime(new Date());
        commentEntity.setCreateTime(new Date());
        commentRepository.save(commentEntity);
    }

    @Override
    public CommentListVo getCommentByReceive(String id, Pageable pageable) {
        CommentListVo commentListVo = new CommentListVo();

        Page<CommentEntity> commentEntities;

        List<CommentVo> commentVos = new ArrayList<>();

        commentEntities = commentRepository.findByReceiverIdOrderByCreateTimeDesc(id,pageable);

        commentListVo.setTotalCount(commentEntities.getTotalElements());

        commentListVo.setTotalCount(commentEntities.getTotalElements());

        for (CommentEntity commentEntity : commentEntities) {
            CommentVo commentVo = new CommentVo();
            BeanUtils.copyProperties(commentEntity, commentVo);
            commentVo.setScore(commentEntity.getPoint());
            TaskEntity taskEntity = taskRepository.findOne(commentEntity.getTaskId());
            commentVo.setTaskTitle(taskEntity.getTitle());
            commentVos.add(commentVo);
        }

        commentListVo.setList(commentVos);

        return commentListVo;
    }

    @Override
    public Double getCommentScoreByReceive(String id) {

        List<CommentEntity> commentEntities;
        Double total = 0.0;

        commentEntities = commentRepository.findByReceiverIdOrderByCreateTimeDesc(id);


        for (CommentEntity commentEntity : commentEntities) {
            total = total + commentEntity.getPoint();
        }

        if (commentEntities.size() == 0) {
            return 0.0;
        } else {
            return total/commentEntities.size();
        }

    }

    @Override
    public CommentListVo getCommentBySend(String id, Pageable pageable) {
        CommentListVo commentListVo = new CommentListVo();

        Page<CommentEntity> commentEntities;

        List<CommentVo> commentVos = new ArrayList<>();

        commentEntities = commentRepository.findByCreatorIdOrderByCreateTimeDesc(id,pageable);

        commentListVo.setTotalCount(commentEntities.getTotalElements());

        for (CommentEntity commentEntity : commentEntities) {
            CommentVo commentVo = new CommentVo();
            BeanUtils.copyProperties(commentEntity, commentVo);
            TaskEntity taskEntity = taskRepository.findOne(commentEntity.getTaskId());
            commentVo.setTaskTitle(taskEntity.getTitle());
            commentVo.setScore(commentEntity.getPoint());
            commentVos.add(commentVo);
        }

        commentListVo.setList(commentVos);

        return commentListVo;
    }

    @Override
    public CommentListVo getCommentListMulti(String creatorId, String receiverId, String type, String taskId, Pageable pageable) {
        Page <CommentEntity> commentEntities;
        List<CommentVo> commentVos = new ArrayList<>();
        CommentListVo commentListVo = new CommentListVo();

        if(creatorId == null && receiverId == null && type == null && taskId == null) {
            commentEntities = commentRepository.findAll(pageable );
        } else {
            commentEntities = commentRepository.findMultList(creatorId, receiverId, type, taskId, pageable );
        }

        for (CommentEntity commentEntity : commentEntities) {
            CommentVo commentVo = new CommentVo();
            BeanUtils.copyProperties(commentEntity, commentVo);
            commentVos.add(commentVo);
        }

        commentListVo.setTotalCount(commentEntities.getTotalElements());
        commentListVo.setList(commentVos);

        return commentListVo;
    }


    @Transactional
    @Override
    public void delComment(DelBody body, UserInfo userInfo) {
        CommentEntity commentEntity = commentRepository.findOne(body.getId());
        if (commentEntity == null) {
            throw new CommonServiceException(ApiResponseCode.DATA_NOT_EXIST.getCode(), ApiResponseCode.DATA_NOT_EXIST.getMsg());
        }
        if (!commentEntity.getCreatorId().equals(userInfo.getId())) {
            throw new CommonServiceException(ApiResponseCode.PERMISSION_DENIED.getCode(), ApiResponseCode.PERMISSION_DENIED.getMsg());
        }
        commentRepository.delete(body.getId());
    }

    @Override
    public void del(DelBody commentId) {
        commentRepository.delete(commentId.getId());
    }

    @Override
    public void delList(DelListBody idList) {
        for(String id : idList.getList()) {
            commentRepository.delete(id);
        }
    }
}
